﻿import QtQuick 2.0

import QtQuick.Window 2.12
import QtQuick.Controls 2.2
import QtQuick.Layouts 1.1
import "../components"

Window {
    id:root
    title: qsTr("修改密码")

    property int  promptMode: 0     // 提示信息 0: 合法密码 1: 密码长度不够 2: 密码包含特殊字符
    function isAlphanumeric(str) {
        return (/^[a-zA-Z0-9]+$/.test(str)) || str.length===0;
    }
    onVisibleChanged: {
        promptMode = 0
        edit_pswd.setText("")
    }


    minimumWidth: 400*dp
    minimumHeight: 250*dp
    maximumWidth: 400*dp
    maximumHeight: 250*dp

    modality: Qt.WindowModal
    flags: Qt.Window | Qt.WindowTitleHint |Qt.WindowSystemMenuHint | Qt.WindowCloseButtonHint


    LineEditor{
        x: 45*dp; y: 68*dp
        id: edit_pswd
        textWidth: 56*dp
        editWidth: 250*dp
        title: "新密码"
        inputMaxLength: 6
        lengthVisible: true
        prompt: "请输入密码..."
        redBorder: promptMode !== 0
        onTextChanged: {
            if(!isAlphanumeric(edit_pswd.getText())){
                promptMode = 2
            }else{
                promptMode = 0
            }
        }
    }
    Item{
        x: 104*dp; y: 118*dp
        width: 220*dp
        Text {
            id: text_prompt
            font.pixelSize: config.fontsize - 2
            color: promptMode === 0 ? "#AAAAAA" : "red"
            text: {
                if(promptMode === 0)    return "请输入6位数字或字母"
                if(promptMode === 1)    return "密码必须为6位"
                if(promptMode === 2)    return "密码不得包含特殊字符"
            }
        }
    }
    Item{
        y: root.height - 90*dp
        width: root.width
        height: 40*dp
        PushButton{
            title: "取消"
            defaultMode2: true
            x: root.width / 2 - 130*dp
            onReleased: {
                root.close()
            }
        }
        PushButton{
            title: "确定"
            defaultMode1: true
            x: root.width / 2 + 10*dp
            onReleased: {
                if(edit_pswd.getText().length !== 6){
                    promptMode = 1
                }else if(!isAlphanumeric(edit_pswd.getText())){
                    promptMode = 2
                }else{
                    let res = CSetUp.editPassWord(edit_pswd.getText())
                    if(res){
                        mainwindow.showToast("修改成功", 0)
                    }else{
                        mainwindow.showToast("修改失败", 1)
                    }
                    root.close()
                }
            }
        }
    }

}
